home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / mm / swap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  22.0 KB  |  922 lines

  1. /*
  2.  *  linux/mm/swap.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file README.legal in the main directory of this archive
  8.  * for more details.
  9.  */
  10.  
  11. /*
  12.  * 680x0 support by Hamish Macdonald
  13.  */
  14.  
  15. /*
  16.  * This file should contain most things doing the swapping from/to disk.
  17.  * Started 18.12.91
  18.  */
  19.  
  20. #include <linux/mm.h>
  21. #include <linux/sched.h>
  22. #include <linux/head.h>
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/string.h>
  26. #include <linux/stat.h>
  27. #include <linux/bootinfo.h>
  28.  
  29. #include <asm/system.h> /* for cli()/sti() */
  30. #include <asm/bitops.h>
  31.  
  32. #define MAX_SWAPFILES 8
  33.  
  34. #define SWP_USED    1
  35. #define SWP_WRITEOK    3
  36.  
  37. #define SWP_TYPE(entry) (((entry) & 0xfe) >> 1)
  38. #define SWP_OFFSET(entry) ((entry) >> PAGE_SHIFT)
  39. #define SWP_ENTRY(type,offset) (((type) << 1) | ((offset) << PAGE_SHIFT))
  40.  
  41. static unsigned int nr_swapfiles = 0;
  42. static struct wait_queue * lock_queue = NULL;
  43.  
  44. static struct swap_info_struct {
  45.     unsigned long flags;
  46.     struct inode * swap_file;
  47.     unsigned int swap_device;
  48.     unsigned char * swap_map;
  49.     char * swap_lockmap;
  50.     unsigned int pages;
  51.     unsigned int lowest_bit;
  52.     unsigned int highest_bit;
  53.     unsigned long max;
  54. } swap_info[MAX_SWAPFILES];
  55.  
  56. extern unsigned long free_page_list;
  57. extern int shm_swap(int);
  58.  
  59. /*
  60.  * The following are used to make sure we don't thrash too much...
  61.  * NOTE!! NR_LAST_FREE_PAGES must be a power of 2...
  62.  */
  63. #define NR_LAST_FREE_PAGES 32
  64. static unsigned long last_free_pages[NR_LAST_FREE_PAGES] = {0,};
  65.  
  66. void rw_swap_page(int rw, unsigned long entry, char * buf)
  67. {
  68.     unsigned long type, offset;
  69.     struct swap_info_struct * p;
  70.  
  71.     type = SWP_TYPE(entry);
  72.     if (type >= nr_swapfiles) {
  73.         printk("Internal error: bad swap-device\n");
  74.         return;
  75.     }
  76.     p = &swap_info[type];
  77.     offset = SWP_OFFSET(entry);
  78.     if (offset >= p->max) {
  79.         printk("rw_swap_page: weirdness\n");
  80.         return;
  81.     }
  82.     if (!(p->flags & SWP_USED)) {
  83.         printk("Trying to swap to unused swap-device\n");
  84.         return;
  85.     }
  86.     while (set_bit(offset,p->swap_lockmap))
  87.         sleep_on(&lock_queue);
  88.     if (p->swap_device) {
  89.         ll_rw_page(rw,p->swap_device,offset,buf);
  90.     } else if (p->swap_file) {
  91.         unsigned int zones[8];
  92.         unsigned int block;
  93.         int i, j;
  94.  
  95.         block = offset << (12 - p->swap_file->i_sb->s_blocksize_bits);
  96.  
  97.         for (i=0, j=0; j< PAGE_SIZE ; i++, j +=p->swap_file->i_sb->s_blocksize)
  98.             if (!(zones[i] = bmap(p->swap_file,block++))) {
  99.                 printk("rw_swap_page: bad swap file\n");
  100.                 return;
  101.             }
  102.         ll_rw_swap_file(rw,p->swap_file->i_dev, zones, i,buf);
  103.     } else
  104.         printk("re_swap_page: no swap file or device\n");
  105.  
  106.     if (rw == READ)
  107.          cache_push_v ((unsigned long)buf, PAGE_SIZE);
  108.  
  109.     if (offset && !clear_bit(offset,p->swap_lockmap))
  110.         printk("rw_swap_page: lock already cleared\n");
  111.     wake_up(&lock_queue);
  112. }
  113.  
  114. unsigned int get_swap_page(void)
  115. {
  116.     struct swap_info_struct * p;
  117.     unsigned int offset, type;
  118.  
  119.     p = swap_info;
  120.     for (type = 0 ; type < nr_swapfiles ; type++,p++) {
  121.         if ((p->flags & SWP_WRITEOK) != SWP_WRITEOK)
  122.             continue;
  123.         for (offset = p->lowest_bit; offset <= p->highest_bit ; offset++) {
  124.             if (p->swap_map[offset])
  125.                 continue;
  126.             p->swap_map[offset] = 1;
  127.             nr_swap_pages--;
  128.             if (offset == p->highest_bit)
  129.                 p->highest_bit--;
  130.             p->lowest_bit = offset;
  131.             return SWP_ENTRY(type,offset);
  132.         }
  133.     }
  134.     return 0;
  135. }
  136.  
  137. unsigned long swap_duplicate(unsigned long entry)
  138. {
  139.     struct swap_info_struct * p;
  140.     unsigned long offset, type;
  141.  
  142.     if (!entry)
  143.         return 0;
  144.     offset = SWP_OFFSET(entry);
  145.     type = SWP_TYPE(entry);
  146.     if (type == SHM_SWP_TYPE)
  147.         return entry;
  148.     if (type >= nr_swapfiles) {
  149.         printk("Trying to duplicate nonexistent swap-page\n");
  150.         return 0;
  151.     }
  152.     p = type + swap_info;
  153.     if (offset >= p->max) {
  154.         printk("swap_free: weirdness\n");
  155.         return 0;
  156.     }
  157.     if (!p->swap_map[offset]) {
  158.         printk("swap_duplicate: trying to duplicate unused page\n");
  159.         return 0;
  160.     }
  161.     p->swap_map[offset]++;
  162.     return entry;
  163. }
  164.  
  165. void swap_free(unsigned long entry)
  166. {
  167.     struct swap_info_struct * p;
  168.     unsigned long offset, type;
  169.  
  170.     if (!entry)
  171.         return;
  172.     type = SWP_TYPE(entry);
  173.     if (type == SHM_SWP_TYPE)
  174.         return;
  175.     if (type >= nr_swapfiles) {
  176.         printk("Trying to free nonexistent swap-page\n");
  177.         return;
  178.     }
  179.     p = & swap_info[type];
  180.     offset = SWP_OFFSET(entry);
  181.     if (offset >= p->max) {
  182.         printk("swap_free: weirdness\n");
  183.         return;
  184.     }
  185.     if (!(p->flags & SWP_USED)) {
  186.         printk("Trying to free swap from unused swap-device\n");
  187.         return;
  188.     }
  189.     while (set_bit(offset,p->swap_lockmap))
  190.         sleep_on(&lock_queue);
  191.     if (offset < p->lowest_bit)
  192.         p->lowest_bit = offset;
  193.     if (offset > p->highest_bit)
  194.         p->highest_bit = offset;
  195.     if (!p->swap_map[offset])
  196.         printk("swap_free: swap-space map bad (entry %08lx)\n",entry);
  197.     else
  198.         if (!--p->swap_map[offset])
  199.             nr_swap_pages++;
  200.     if (!clear_bit(offset,p->swap_lockmap))
  201.         printk("swap_free: lock already cleared\n");
  202.     wake_up(&lock_queue);
  203. }
  204.  
  205. void swap_in(unsigned long *table_ptr)
  206. {
  207.     unsigned long entry;
  208.     unsigned long page;
  209.  
  210.     entry = *table_ptr;
  211. /*    dbprintf ("process %d swapping in entry %ld\n", current->pid,
  212.           entry); */
  213.  
  214.     if (PAGE_PRESENT & entry) {
  215.         printk("trying to swap in present page\n");
  216.         return;
  217.     }
  218.     if (!entry) {
  219.         printk("No swap page in swap_in\n");
  220.         return;
  221.     }
  222.     if (SWP_TYPE(entry) == SHM_SWP_TYPE) {
  223.         shm_no_page ((unsigned long *) table_ptr);
  224.         return;
  225.     }
  226.     if (!(page = get_free_page(GFP_KERNEL))) {
  227.         oom(current);
  228.         page = BAD_PAGE;
  229.     } else
  230.         read_swap_page(entry, (char *) page);
  231.     if (*table_ptr != entry) {
  232.         free_page(page);
  233.         return;
  234.     }
  235.     invalidate();
  236.     *table_ptr = VTOP(page) | (PAGE_DIRTY | PAGE_PRIVATE);
  237.     swap_free(entry);
  238. }
  239.  
  240. static inline int try_to_swap_out(unsigned long * table_ptr)
  241. {
  242.     int i;
  243.     unsigned long page;
  244.     unsigned long page_virt;
  245.     unsigned long entry;
  246.  
  247.     page = *table_ptr;
  248.     if (!(PAGE_PRESENT & page))
  249.     return 0;
  250.     page_virt = PTOV(page & PAGE_MASK);
  251.     if (page_virt >= high_memory) {
  252.     return 0;
  253.     }
  254.     if (mem_map[MAP_NR(page_virt)] & MAP_PAGE_RESERVED)
  255.     return 0;
  256.     if (PAGE_ACCESSED & page) {
  257.     *table_ptr &= ~PAGE_ACCESSED;
  258.     invalidate ();
  259.     return 0;
  260.     }
  261.     for (i = 0; i < NR_LAST_FREE_PAGES; i++)
  262.     if (last_free_pages[i] == page_virt)
  263.         return 0;
  264.     if (PAGE_DIRTY & page) {
  265.     if (mem_map[MAP_NR(page_virt)] != 1)
  266.         return 0;
  267.     if (!(entry = get_swap_page()))
  268.         return 0;
  269.     *table_ptr = entry;
  270.     invalidate();
  271.     write_swap_page(entry, (char *) page_virt);
  272.     free_page(page_virt);
  273.     return 1;
  274.     }
  275.     *table_ptr = 0;
  276.     invalidate();
  277.     free_page(page_virt);
  278.     return 1 + mem_map[MAP_NR(page_virt)];
  279. }
  280.  
  281. /*
  282.  * A new implementation of swap_out().  We do not swap complete processes,
  283.  * but only a small number of blocks, before we continue with the next
  284.  * process.  The number of blocks actually swapped is determined on the
  285.  * number of page faults, that this process actually had in the last time,
  286.  * so we won't swap heavily used processes all the time ...
  287.  *
  288.  * Note: the priority argument is a hint on much CPU to waste with the
  289.  *     swap block search, not a hint, of how much blocks to swap with
  290.  *     each process.
  291.  *
  292.  * (C) 1993 Kai Petzke, wpp@marie.physik.tu-berlin.de
  293.  */
  294. #ifdef NEW_SWAP
  295. /*
  296.  * These are the miminum and maximum number of pages to swap from one process,
  297.  * before proceeding to the next:
  298.  */
  299. #define SWAP_MIN    4
  300. #define SWAP_MAX    32
  301.  
  302. /*
  303.  * The actual number of pages to swap is determined as:
  304.  * SWAP_RATIO / (number of recent major page faults)
  305.  */
  306. #define SWAP_RATIO    128
  307.  
  308. #define L1_SWAP_INDEX(root,table) ((root)[(table)>>3])
  309. #define L2_SWAP_INDEX(ptr,table) ((ptr)[((table)&0x7)*16])
  310.  
  311. static int swap_out(unsigned int priority)
  312. {
  313.     static int swap_task;
  314.     int table;
  315.     int page;
  316.     long pg_table;
  317.     int loop;
  318.     int counter = NR_TASKS * 2 >> priority;
  319.     struct task_struct *p;
  320.  
  321.     counter = NR_TASKS * 2 >> priority;
  322.     for(; counter >= 0; counter--, swap_task++) {
  323.     /*
  324.      * Check that swap_task is suitable for swapping.  If not, look for
  325.      * the next suitable process.
  326.      */
  327.     loop = 0;
  328.     while(1) {
  329.         if(swap_task >= NR_TASKS) {
  330.         swap_task = 1;
  331.         if(loop)
  332.             /* all processes are unswappable or already swapped out */
  333.             return 0;
  334.         loop = 1;
  335.         }
  336.  
  337.         p = task[swap_task];
  338.         if(p && p->swappable && p->rss)
  339.         break;
  340.  
  341.         swap_task++;
  342.     }
  343.  
  344.     /*
  345.      * Determine the number of pages to swap from this process.
  346.      */
  347.     if(! p -> swap_cnt) {
  348.         p->dec_flt = (p->dec_flt * 3) / 4 + p->maj_flt - p->old_maj_flt;
  349.         p->old_maj_flt = p->maj_flt;
  350.  
  351.         if(p->dec_flt >= SWAP_RATIO / SWAP_MIN) {
  352.         p->dec_flt = SWAP_RATIO / SWAP_MIN;
  353.         p->swap_cnt = SWAP_MIN;
  354.         } else if(p->dec_flt <= SWAP_RATIO / SWAP_MAX)
  355.         p->swap_cnt = SWAP_MAX;
  356.         else
  357.         p->swap_cnt = SWAP_RATIO / p->dec_flt;
  358.     }
  359.  
  360.     /*
  361.      * Go through process' page directory.
  362.      */
  363.     for(table = p->swap_table; table < 1024; table++) {
  364.         unsigned long desc, *ptr;
  365.  
  366.         desc = L1_SWAP_INDEX(p->tss.pagedir_v,table);
  367.         if(!desc || PTOV(desc & TABLE_MASK) >= high_memory)
  368.             continue;
  369.         if(!(PAGE_TABLE & desc)) {
  370.             printk("swap_out: bad page-table at pg_dir[%d]: %08lx\n",
  371.                 table, desc);
  372.             L1_SWAP_INDEX(p->tss.pagedir_v,table) = 0;
  373.             continue;
  374.         }
  375.         ptr = (unsigned long *)PTOV(desc & TABLE_MASK);
  376.         pg_table = L2_SWAP_INDEX(ptr,table);
  377.         if (!pg_table || PTOV(pg_table & PAGE_MASK) >= high_memory)
  378.             continue;
  379.         if(mem_map[MAP_NR(PTOV(pg_table & PAGE_MASK))] & MAP_PAGE_RESERVED)
  380.             continue;
  381.         if(!(PAGE_TABLE & pg_table)) {
  382.             printk("swap_out: bad page-table at pg_dir[%d]: %08lx\n",
  383.                 table, pg_table);
  384.             L2_SWAP_INDEX(ptr,table) = 0;
  385.             continue;
  386.         }
  387.         pg_table = PTOV(pg_table & PAGE_MASK);
  388.  
  389.         /*
  390.          * Go through this page table.
  391.          */
  392.         for(page = p->swap_page; page < 1024; page++) {
  393.         switch(try_to_swap_out(page + (unsigned long *) pg_table)) {
  394.             case 0:
  395.             break;
  396.  
  397.             case 1:
  398.             p->rss--;
  399.             /* continue with the following page the next time */
  400.             p->swap_table = table;
  401.             p->swap_page  = page + 1;
  402.             if((--p->swap_cnt) == 0)
  403.                 swap_task++;
  404. /*            dbprintf ("swapped out a page from pid %d\n", p->pid); */
  405.             return 1;
  406.  
  407.             default:
  408.             p->rss--;
  409.             break;
  410.         }
  411.         }
  412.  
  413.         p->swap_page = 0;
  414.     }
  415.  
  416.     /*
  417.      * Finish work with this process, if we reached the end of the page
  418.      * directory.  Mark restart from the beginning the next time.
  419.      */
  420.     p->swap_table = 0;
  421.     }
  422.     return 0;
  423. }
  424.  
  425. #else /* old swapping procedure */
  426.  
  427. /*
  428.  * Go through the page tables, searching for a user page that
  429.  * we can swap out.
  430.  *
  431.  * We now check that the process is swappable (normally only 'init'
  432.  * is un-swappable), allowing high-priority processes which cannot be
  433.  * swapped out (things like user-level device drivers (Not implemented)).
  434.  */
  435. static int swap_out(unsigned int priority)
  436. {
  437.     static int swap_task = 1;
  438.     static int swap_table = 0;
  439.     static int swap_page = 0;
  440.     int counter = NR_TASKS*8;
  441.     unsigned long pg_table, pg_table_v, desc, *ptr;
  442.     struct task_struct * p;
  443.  
  444.     counter >>= priority;
  445. check_task:
  446.     if (counter-- < 0)
  447.     return 0;
  448.     if (swap_task >= NR_TASKS) {
  449.     swap_task = 1;
  450.     goto check_task;
  451.     }
  452.     p = task[swap_task];
  453.     if (!p || !p->swappable) {
  454.     swap_task++;
  455.     goto check_task;
  456.     }
  457. check_dir:
  458.     if (swap_table >= PTRS_PER_PAGE) {
  459.     swap_table = 0;
  460.     swap_task++;
  461.     goto check_task;
  462.     }
  463.     desc = L1_SWAP_INDEX(p->tss.pagedir_v,swap_table);
  464.     if (!desc) {
  465.         swap_table++;
  466.         goto check_dir;
  467.     }
  468.     ptr = (unsigned long *)PTOV(desc & TABLE_MASK);
  469.     if ((unsigned long)ptr >= high_memory || (mem_map[MAP_NR(ptr)] & MAP_PAGE_RESERVED)) {
  470.     swap_table++;
  471.     goto check_dir;
  472.     }
  473.     if (!(PAGE_TABLE & desc)) {
  474.     printk("bad page-table at pg_dir[%d]: %08x\n",
  475.            swap_table,pg_table);
  476.     L1_SWAP_INDEX(p->tss.pagedir_v,swap_table) = 0;
  477.     swap_table++;
  478.     goto check_dir;
  479.     }
  480.     pg_table = L2_SWAP_INDEX(ptr,swap_table);
  481.     if (!pg_table) {
  482.         swap_table++;
  483.         goto check_dir;
  484.     }
  485.     pg_table_v = PTOV(pg_table & PAGE_MASK);
  486.     if (pg_table_v >= high_memory || (mem_map[MAP_NR(pg_table_v)] & MAP_PAGE_RESERVED)) {
  487.     swap_table++;
  488.     goto check_dir;
  489.     }
  490.     if (!(PAGE_TABLE & pg_table)) {
  491.     printk("bad page-table at pg_dir[%d]: %08x\n",
  492.            swap_table,pg_table);
  493.     L2_SWAP_INDEX(ptr,swap_table) = 0;
  494.     swap_table++;
  495.     goto check_dir;
  496.     }
  497.   check_table:
  498.     if (swap_page >= PTRS_PER_PAGE) {
  499.     swap_page = 0;
  500.     swap_table++;
  501.     goto check_dir;
  502.     }
  503.     switch (try_to_swap_out(swap_page + (unsigned long *) pg_table_v)) {
  504.       case 0: break;
  505.       case 1: p->rss--; return 1;
  506.       default: p->rss--;
  507.     }
  508.     swap_page++;
  509.     goto check_table;
  510. }
  511. #endif /* !NEW_SWAP */
  512.  
  513. /*
  514.  * sys_idle() does nothing much: it just searches for likely candidates for
  515.  * swapping out or forgetting about. This speeds up the search when we
  516.  * actually have to swap.
  517.  */
  518. asmlinkage int sys_idle(void)
  519. {
  520.     need_resched = 1;
  521.     return 0;
  522. }
  523.  
  524. static int try_to_free_page(void)
  525. {
  526.     int i=6;
  527.  
  528.     while (i--) {
  529.         if (shrink_buffers(i))
  530.             return 1;
  531.         if (shm_swap(i))
  532.             return 1;
  533.         if (swap_out(i))
  534.             return 1;
  535.     }
  536.     return 0;
  537. }
  538.  
  539. /*
  540.  * Note that this must be atomic, or bad things will happen when
  541.  * pages are requested in interrupts (as malloc can do). Thus the
  542.  * cli/sti's.
  543.  */
  544. static inline void add_mem_queue(unsigned long addr, unsigned long * queue)
  545. {
  546.     addr &= PAGE_MASK;
  547.     *(unsigned long *) addr = *queue;
  548.     *queue = addr;
  549. }
  550.  
  551. /*
  552.  * Free_page() adds the page to the free lists. This is optimized for
  553.  * fast normal cases (no error jumps taken normally).
  554.  *
  555.  * The way to optimize jumps for gcc-2.2.2 is to:
  556.  *  - select the "normal" case and put it inside the if () { XXX }
  557.  *  - no else-statements if you can avoid them
  558.  *
  559.  * With the above two rules, you get a straight-line execution path
  560.  * for the normal case, giving better asm-code.
  561.  */
  562. void free_page(unsigned long addr)
  563. {
  564.     if (addr >= KSTART_ADDR && addr < high_memory) {
  565.         unsigned short * map = mem_map + MAP_NR(addr);
  566.  
  567.         if (*map) {
  568.             if (!(*map & MAP_PAGE_RESERVED)) {
  569.                 unsigned long flag;
  570.  
  571.                 save_flags(flag);
  572.                 cli();
  573.                 if (!--*map) {
  574.                     if (nr_secondary_pages < MAX_SECONDARY_PAGES) {
  575.                         add_mem_queue(addr,&secondary_page_list);
  576.                         nr_secondary_pages++;
  577.                         restore_flags(flag);
  578.                         return;
  579.                     }
  580.                     add_mem_queue(addr,&free_page_list);
  581.                     nr_free_pages++;
  582.                 }
  583.                 restore_flags(flag);
  584.             }
  585.             return;
  586.         }
  587.         printk("Trying to free free memory (%08lx): memory probabably corrupted\n",addr);
  588.         printk("PC = %08lx\n",*(((unsigned long *)&addr)-1));
  589.         return;
  590.     }
  591.     return;
  592. }
  593.  
  594. /*
  595.  * This is one ugly macro, but it simplifies checking, and makes
  596.  * this speed-critical place reasonably fast, especially as we have
  597.  * to do things with the interrupt flag etc.
  598.  *
  599.  * Note that this #define is heavily optimized to give fast code
  600.  * for the normal case - the if-statements are ordered so that gcc-2.2.2
  601.  * will make *no* jumps for the normal code. Don't touch unless you
  602.  * know what you are doing.
  603.  */
  604. #define REMOVE_FROM_MEM_QUEUE(queue,nr) \
  605.     cli(); \
  606.     if ((result = queue) != 0) { \
  607.         if (!(result & ~PAGE_MASK) && result < high_memory) { \
  608.             queue = *(unsigned long *) result; \
  609.             if (!mem_map[MAP_NR(result)]) { \
  610.                 mem_map[MAP_NR(result)] = 1; \
  611.                 nr--; \
  612. last_free_pages[index = (index + 1) & (NR_LAST_FREE_PAGES - 1)] = result; \
  613.                 restore_flags(flag); \
  614.                 /*             \
  615.                  * invalidate instruction cache for page \
  616.                  * if 68040         \
  617.                  */             \
  618.                 if (boot_info.cputype & CPU_68040) \
  619.                     asm volatile ("movel %0,a0\n\t" \
  620.                               /* CPUSHP I/D (a0) */ \
  621.                               ".word 0xf490"    \
  622.                               : : "g" (VTOP(result)) \
  623.                               : "a0");          \
  624.                 return result; \
  625.             } \
  626.             printk("Free page %08lx has mem_map = %d\n", \
  627.                 result,mem_map[MAP_NR(result)]); \
  628.         } else \
  629.             printk("Result = 0x%08lx - memory map destroyed\n", result); \
  630.         queue = 0; \
  631.         nr = 0; \
  632.     } else if (nr) { \
  633.         printk(#nr " is %d, but " #queue " is empty\n",nr); \
  634.         nr = 0; \
  635.     } \
  636.     restore_flags(flag)
  637.  
  638. /*
  639.  * Get physical address of first (actually last :-) free page, and mark it
  640.  * used. If no free pages left, return 0.
  641.  *
  642.  * Note that this is one of the most heavily called functions in the kernel,
  643.  * so it's a bit timing-critical (especially as we have to disable interrupts
  644.  * in it). See the above macro which does most of the work, and which is
  645.  * optimized for a fast normal path of execution.
  646.  */
  647. unsigned long __get_free_page(int priority)
  648. {
  649.     unsigned long result, flag;
  650.     static unsigned long index = 0;
  651.  
  652.     /* this routine can be called at interrupt time via
  653.        malloc.  We want to make sure that the critical
  654.        sections of code have interrupts disabled. -RAB
  655.        Is this code reentrant? */
  656.  
  657.     save_flags(flag);
  658. repeat:
  659.     REMOVE_FROM_MEM_QUEUE(free_page_list,nr_free_pages);
  660.     if (priority == GFP_BUFFER)
  661.         return 0;
  662.     if (priority != GFP_ATOMIC) {
  663.         if (try_to_free_page())
  664.             goto repeat;
  665.     }
  666.     REMOVE_FROM_MEM_QUEUE(secondary_page_list,nr_secondary_pages);
  667.     return 0;
  668. }
  669.  
  670. /*
  671.  * Trying to stop swapping from a file is fraught with races, so
  672.  * we repeat quite a bit here when we have to pause. swapoff()
  673.  * isn't exactly timing-critical, so who cares?
  674.  */
  675. static int try_to_unuse(unsigned int type)
  676. {
  677.     int nr, pgt, pg;
  678.     unsigned long page, *ppage, desc, *ptr;
  679.     unsigned long tmp = 0;
  680.     struct task_struct *p;
  681.  
  682.     nr = 0;
  683. /*
  684.  * When we have to sleep, we restart the whole algorithm from the same
  685.  * task we stopped in. That at least rids us of all races.
  686.  */
  687. repeat:
  688.     for (; nr < NR_TASKS ; nr++) {
  689.         p = task[nr];
  690.         if (!p)
  691.             continue;
  692.         for (pgt = 0 ; pgt < PTRS_PER_PAGE ; pgt++) {
  693.             desc = L1_SWAP_INDEX(p->tss.pagedir_v,pgt);
  694.             if (!desc)
  695.                 continue;
  696.             if (!(desc & PAGE_TABLE) || PTOV(desc & TABLE_MASK) >= high_memory)
  697.                 continue;
  698.             ptr = (unsigned long *)PTOV(desc & TABLE_MASK);
  699.             page = L2_SWAP_INDEX(ptr,pgt);
  700.             if (!page)
  701.                 continue;
  702.             if (!(page & PAGE_PRESENT) || (PTOV(page & PAGE_MASK) >= high_memory))
  703.                 continue;
  704.             if (mem_map[MAP_NR(PTOV(page & PAGE_MASK))] & MAP_PAGE_RESERVED)
  705.                 continue;
  706.             ppage = (unsigned long *) PTOV(page & PAGE_MASK);
  707.             for (pg = 0 ; pg < PTRS_PER_PAGE ; pg++,ppage++) {
  708.                 page = *ppage;
  709.                 if (!page)
  710.                     continue;
  711.                 if (page & PAGE_PRESENT)
  712.                     continue;
  713.                 if (SWP_TYPE(page) != type)
  714.                     continue;
  715.                 if (!tmp) {
  716.                     if (!(tmp = __get_free_page(GFP_KERNEL))) {
  717.                         invalidate();
  718.                         return -ENOMEM;
  719.                     }
  720.                     goto repeat;
  721.                 }
  722.                 read_swap_page(page, (char *) tmp);
  723.                 if (*ppage == page) {
  724.                     *ppage = VTOP(tmp) | (PAGE_DIRTY | PAGE_PRIVATE);
  725.                     ++p->rss;
  726.                     swap_free(page);
  727.                     tmp = 0;
  728.                 }
  729.                 goto repeat;
  730.             }
  731.         }
  732.     }
  733.     free_page(tmp);
  734.     invalidate();
  735.     return 0;
  736. }
  737.  
  738. asmlinkage int sys_swapoff(const char * specialfile)
  739. {
  740.     struct swap_info_struct * p;
  741.     struct inode * inode;
  742.     unsigned int type;
  743.     int i;
  744.  
  745.     if (!suser())
  746.         return -EPERM;
  747.     i = namei(specialfile,&inode);
  748.     if (i)
  749.         return i;
  750.     p = swap_info;
  751.     for (type = 0 ; type < nr_swapfiles ; type++,p++) {
  752.         if ((p->flags & SWP_WRITEOK) != SWP_WRITEOK)
  753.             continue;
  754.         if (p->swap_file) {
  755.             if (p->swap_file == inode)
  756.                 break;
  757.         } else {
  758.             if (!S_ISBLK(inode->i_mode))
  759.                 continue;
  760.             if (p->swap_device == inode->i_rdev)
  761.                 break;
  762.         }
  763.     }
  764.     iput(inode);
  765.     if (type >= nr_swapfiles)
  766.         return -EINVAL;
  767.     p->flags = SWP_USED;
  768.     i = try_to_unuse(type);
  769.     if (i) {
  770.         p->flags = SWP_WRITEOK;
  771.         return i;
  772.     }
  773.     nr_swap_pages -= p->pages;
  774.     iput(p->swap_file);
  775.     p->swap_file = NULL;
  776.     p->swap_device = 0;
  777.     vfree(p->swap_map);
  778.     p->swap_map = NULL;
  779.     free_page((long) p->swap_lockmap);
  780.     p->swap_lockmap = NULL;
  781.     p->flags = 0;
  782.     return 0;
  783. }
  784.  
  785. /*
  786.  * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
  787.  *
  788.  * The swapon system call
  789.  */
  790. asmlinkage int sys_swapon(const char * specialfile)
  791. {
  792.     struct swap_info_struct * p;
  793.     struct inode * swap_inode;
  794.     unsigned int type;
  795.     int i,j;
  796.     int error;
  797.  
  798.     if (!suser())
  799.         return -EPERM;
  800.     p = swap_info;
  801.     for (type = 0 ; type < nr_swapfiles ; type++,p++)
  802.         if (!(p->flags & SWP_USED))
  803.             break;
  804.     if (type >= MAX_SWAPFILES)
  805.         return -EPERM;
  806.     if (type >= nr_swapfiles)
  807.         nr_swapfiles = type+1;
  808.     p->flags = SWP_USED;
  809.     p->swap_file = NULL;
  810.     p->swap_device = 0;
  811.     p->swap_map = NULL;
  812.     p->swap_lockmap = NULL;
  813.     p->lowest_bit = 0;
  814.     p->highest_bit = 0;
  815.     p->max = 1;
  816.     error = namei(specialfile,&swap_inode);
  817.     if (error)
  818.         goto bad_swap;
  819.     error = -EBUSY;
  820.     if (swap_inode->i_count != 1)
  821.         goto bad_swap;
  822.     error = -EINVAL;
  823.     if (S_ISBLK(swap_inode->i_mode)) {
  824.         p->swap_device = swap_inode->i_rdev;
  825.         iput(swap_inode);
  826.         error = -ENODEV;
  827.         if (!p->swap_device)
  828.             goto bad_swap;
  829.         error = -EBUSY;
  830.         for (i = 0 ; i < nr_swapfiles ; i++) {
  831.             if (i == type)
  832.                 continue;
  833.             if (p->swap_device == swap_info[i].swap_device)
  834.                 goto bad_swap;
  835.         }
  836.     } else if (S_ISREG(swap_inode->i_mode))
  837.         p->swap_file = swap_inode;
  838.     else
  839.         goto bad_swap;
  840.     p->swap_lockmap = (unsigned char *) get_free_page(GFP_USER);
  841.     if (!p->swap_lockmap) {
  842.         printk("Unable to start swapping: out of memory :-)\n");
  843.         error = -ENOMEM;
  844.         goto bad_swap;
  845.     }
  846.     read_swap_page(SWP_ENTRY(type,0), (char *) p->swap_lockmap);
  847.     if (memcmp("SWAP-SPACE",p->swap_lockmap+4086,10)) {
  848.         printk("Unable to find swap-space signature\n");
  849.         error = -EINVAL;
  850.         goto bad_swap;
  851.     }
  852.     memset(p->swap_lockmap+PAGE_SIZE-10,0,10);
  853.     j = 0;
  854.     p->lowest_bit = 0;
  855.     p->highest_bit = 0;
  856.     for (i = 1 ; i < 8*PAGE_SIZE ; i++) {
  857.         if (test_bit(i,p->swap_lockmap)) {
  858.             if (!p->lowest_bit)
  859.                 p->lowest_bit = i;
  860.             p->highest_bit = i;
  861.             p->max = i+1;
  862.             j++;
  863.         }
  864.     }
  865.     if (!j) {
  866.         printk("Empty swap-file\n");
  867.         error = -EINVAL;
  868.         goto bad_swap;
  869.     }
  870.     p->swap_map = (unsigned char *) vmalloc(p->max);
  871.     if (!p->swap_map) {
  872.         error = -ENOMEM;
  873.         goto bad_swap;
  874.     }
  875.     for (i = 1 ; i < p->max ; i++) {
  876.         if (test_bit(i,p->swap_lockmap))
  877.             p->swap_map[i] = 0;
  878.         else
  879.             p->swap_map[i] = 0x80;
  880.     }
  881.     p->swap_map[0] = 0x80;
  882.     memset(p->swap_lockmap,0,PAGE_SIZE);
  883.     p->flags = SWP_WRITEOK;
  884.     p->pages = j;
  885.     nr_swap_pages += j;
  886.     printk("Adding Swap: %dk swap-space\n",j<<2);
  887.     return 0;
  888. bad_swap:
  889.     free_page((long) p->swap_lockmap);
  890.     vfree(p->swap_map);
  891.     iput(p->swap_file);
  892.     p->swap_device = 0;
  893.     p->swap_file = NULL;
  894.     p->swap_map = NULL;
  895.     p->swap_lockmap = NULL;
  896.     p->flags = 0;
  897.     return error;
  898. }
  899.  
  900. void si_swapinfo(struct sysinfo *val)
  901. {
  902.     unsigned int i, j;
  903.  
  904.     val->freeswap = val->totalswap = 0;
  905.     for (i = 0; i < nr_swapfiles; i++) {
  906.         if (!(swap_info[i].flags & SWP_USED))
  907.             continue;
  908.         for (j = 0; j < swap_info[i].max; ++j)
  909.             switch (swap_info[i].swap_map[j]) {
  910.                 case 128:
  911.                     continue;
  912.                 case 0:
  913.                     ++val->freeswap;
  914.                 default:
  915.                     ++val->totalswap;
  916.             }
  917.     }
  918.     val->freeswap <<= PAGE_SHIFT;
  919.     val->totalswap <<= PAGE_SHIFT;
  920.     return;
  921. }
  922.